$(function () {
	$("input[name='chk_roomStatus']").each(function(){
		var roomStatus = $("input[name='roomStatus']").val();
		var chkValue = $(this).val();
		if(roomStatus){
			var chk_boolean = roomStatus.indexOf(chkValue)>=0;
			if(chk_boolean){
				$(this).prop("checked", chk_boolean);
			}
			
		}
	})
	
	//修改房间
	$("#btn_roomUpdate").on("click", function(){
		var roomId = $("input[name='roomId']").val();
		var parentName = $("input[name='parentName']").val();
		var phtMap = $("input[name='phtMap']").val();
		roomUpdate(roomId, parentName, phtMap);
	});
	
	//删除房间
	$("#btn_roomDel").on("click", function(){
		var roomId = $("input[name='roomId']").val();
		del(roomId);
	});
	
});

//房间修改弹出层
function roomUpdate(roomId, parentName, phtMap){
	JY.Ajax.doRequest(null,jypath+'/estate/projectRoom/view',{id:roomId},function(data){
		setRoomForm(data, phtMap);
		$("#roomForm label[name='parentName']").text(parentName);
    	save("修改房间","/estate/projectRoom/edit", roomId);
	});
}
function save(title, url, id){
	JY.Model.editBig("roomDiv",title,function(){
		if(JY.Validate.form("roomForm")){
			var that =$(this);
			JY.Ajax.doRequest("roomForm",jypath +url,null,function(data){	
				that.dialog("close");
	        	JY.Model.info(data.resMsg,function(){
	        		var url = jypath + "/estate/projectRoom/detail?id="+id;
	        		$.post(url, null, function(data){
	        			$("#rightContentDiv").html(data);
	        		});
        		});    
			 });
		}	
	});
}

function del(id){
 	JY.Model.confirm("确认删除此房间？",function(){	
 		JY.Ajax.doRequest(null,jypath+'/estate/projectRoom/delete',{id:id},function(data){
 			JY.Model.info(data.resMsg,function(){
 				$("#rightContentDiv").html("");
 				if(data.res == 1){
 	 				deleteNode();
 	 			}
 			});
 		});
 	});
}

function setRoomForm(data, phtMap){
	var obj = data.obj;
	$("#roomForm input[name$='id']").val(JY.Object.notEmpty(obj.id));
	$("#roomForm input[name$='roomName']").val(JY.Object.notEmpty(obj.roomName));
	$("#roomForm input[name$='roomCode']").val(JY.Object.notEmpty(obj.roomCode));
	$("#roomForm input[name$='floor']").val(JY.Object.notEmpty(obj.floor));
	$("#roomForm select[name$='propertyType']").val(JY.Object.notEmpty(obj.propertyType));
	$("#roomForm input[name$='billingArea']").val(JY.Object.notEmpty(obj.billingArea));
	$("#roomForm input[name$='buildingArea']").val(JY.Object.notEmpty(obj.buildingArea));
	$("#roomForm input[name$='innerArea']").val(JY.Object.notEmpty(obj.innerArea));
	$("#roomForm input[name$='sharingArea']").val(JY.Object.notEmpty(obj.sharingArea));
	$("#roomForm input[name$='gardenArea']").val(JY.Object.notEmpty(obj.gardenArea));
	$("#roomForm input[name$='basementArea']").val(JY.Object.notEmpty(obj.basementArea));
	$("#roomForm input[name$='freeArea']").val(JY.Object.notEmpty(obj.freeArea));
	if(obj.roomStatus){
		$("#roomForm input[name='chk_roomStatus']").each(function(){
			var roomStatus = obj.roomStatus;
			var chkValue = $(this).val();
			var chk_boolean = false;
			var indexLength = roomStatus.indexOf(chkValue);
			if(indexLength >= 0){
				chk_boolean = true;
			}
			$(this).prop("checked", chk_boolean);
		})
	}
	$("#roomForm input[name$='salesPermissionNo']").val(JY.Object.notEmpty(obj.salesPermissionNo));
	$("#roomForm input[name$='salesPermissionDateOrigin']").val(JY.Object.notEmpty(JY.Date.Format(obj.salesPermissionDate,"yyyy-MM-dd")));
	$("#roomForm textarea[name$='remark']").val(JY.Object.notEmpty(obj.remark));
	var options = "";
	if(phtMap){
		var startIndex=phtMap.indexOf("{");
		var endIndex=phtMap.lastIndexOf("}");
	    if(startIndex!==-1 && endIndex!==-1){  
	    	phtMap = phtMap.substring(startIndex + 1,endIndex);  
	        var phtArrs= phtMap.split(",");  
	        for(var ii=0; ii<phtArrs.length; ii++){  
	            var pht = phtArrs[ii].trim();  
	            if(pht.length > 0 && pht.indexOf("=") !== -1){  
	                var pht_arr = pht.split("=");  
	                if(pht_arr.length == 2){
	                	if(obj.houseTypeId && obj.houseTypeId == pht_arr[0]){
	                		options = "<option selected='selected' value='"+pht_arr[0]+"'>"+pht_arr[1]+"</option>";
	                	}else{
	                		options = "<option value='"+pht_arr[0]+"'>"+pht_arr[1]+"</option>";
	                	}
	                	$("#roomForm select[name$='houseTypeId']").append(options);
	                }  
	            }  
	        }  
	    }
	}
}

